#include <algorithm>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <list>
#include <bitset>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <cassert>
#include <ctime>
#include <numeric>
#include <functional>
#include <valarray>
#include <complex>
#include <utility>
using namespace std;

const int dx[]={0,-1,0,1,-1,1, 1,-1};
const int dy[]={1,0,-1,0, 1,1,-1,-1};
const double eps=1e-8;

typedef long long LL;

typedef pair<int,int> pii;
typedef vector<int> vi;

typedef vector<string> vs;

#define sz(a) (int)a.size()
#define fori(i,a,b) for(int i(a),_b(b);i<=_b;++i)

#define forn(i,n) fori(i,0,n-1)
#define fora(i,a) forn(i,sz(a))

#define all(a) a.begin(),a.end()

#define pb push_back

#define two(X) (1LL<<(X))

#define mmax 80

#define debugprint(a) cout << a << ' '
template <typename T>
void debugprintarr(T arr,int x, int y)
{
    fori(i,x,y) debugprint(arr[i]);
    cout << '\n';
}

template <typename T>
void debugprint2darr(T arr, int x1, int y1, int x2, int y2)
{
    fori(i,x1,y1) debugprintarr(arr[i],x2,y2);
}

struct mycoor
{
	int x1,y1,x2,y2;
};
struct myseg
{
	int l,r;
};
int total;
typedef vector<mycoor> vmc;
typedef vector<myseg> vms;
bool mysegcomp(myseg i,myseg j)
{
	return (i.l<j.l || (i.l==j.l && i.r<j.r));
}
void input(vs hier,vms linehor[mmax+1],vms linever[mmax+1])
{
	int num,numar[4],cnt;
	fora(i,hier)
	{
		hier[i]+=' ';
		num=cnt=0;
		fora(j,hier[i])
		{
			if (hier[i][j]==' ' || hier[i][j]==',')
			{
				numar[cnt++]=num;
				num=0;
			}
			else
				num=num*10+hier[i][j]-'0';
			if (cnt==4)
			{
				if (numar[0]>numar[2]) swap(numar[0],numar[2]);
				if (numar[1]>numar[3]) swap(numar[1],numar[3]);
				if (numar[0]==numar[2])
				{
                    myseg tmp;
                    tmp.l=numar[1];
                    tmp.r=numar[3];
                    linever[numar[0]].pb(tmp);
				}
				else
				if (numar[1]==numar[3])
                {
                    myseg tmp;
                    tmp.l=numar[0];
                    tmp.r=numar[2];
                    linehor[numar[1]].pb(tmp);
                }
				total+=numar[2]-numar[0]+numar[3]-numar[1];
				cnt=0;
			}
		}
	}
}
void tpsort(vms seg[mmax+1])
{
	fori(i,0,mmax)
		if (!seg[i].empty())
			sort(all(seg[i]),mysegcomp);
}
void exam(myseg a,myseg b,int &i,int &j,int &sum)
{
	//a.l+=dt2; a.r+=dt2;
	if (b.l<a.r)
	{
		if (a.r<b.r)
		{
			sum+=a.r-b.l;
			i++;
		} else
		{
			sum+=b.r-b.l;
			j++;
		}
	} else i++;
}
int overlap(vms seg1[mmax+1],vms seg2[mmax+1],int dt1,int dt2)
{
	int sum=0;
	fori(i,0,mmax)
	if ( i+dt1>=0 && i+dt1<=mmax && !seg1[i+dt1].empty() && !seg2[i].empty() )
	{

		int u=0,v=0;
		while (u<sz(seg1[i+dt1]) && v<sz(seg2[i]))
		{
			myseg a=seg1[i+dt1][u];
			a.l-=dt2; a.r-=dt2;
			if (a.l<seg2[i][v].l)
				exam(a,seg2[i][v],u,v,sum);
			else
				exam(seg2[i][v],a,v,u,sum);
		}
	}
	return sum;
}

class Hieroglyphs {

public:
	int minimumVisible(vs hier1, vs hier2) {
		vms line1hor[mmax+1],line1ver[mmax+1],line2hor[mmax+1],line2ver[mmax+1];
		total=0;
		input(hier1,line1hor,line1ver);
		input(hier2,line2hor,line2ver);
		tpsort(line1hor);
		tpsort(line2hor);
		tpsort(line1ver);
		tpsort(line2ver);
		int kq=0;
		fori(i,-mmax,mmax)
			fori(j,-mmax,mmax)
			{
				int tmp1=overlap(line1hor,line2hor,i,j);
				int tmp2=overlap(line1ver,line2ver,j,i);
				kq=max(kq,tmp1+tmp2);
			}
        //cout << total << '\n';
        //cout << kq << '\n';
		return total-kq;
	}
};



// BEGIN KAWIGIEDIT TESTING
// Generated by KawigiEdit-pf 2.3.0
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <cmath>
using namespace std;
bool KawigiEdit_RunTest(int testNum, vector <string> p0, vector <string> p1, bool hasAnswer, int p2) {
	cout << "Test " << testNum << ": [" << "{";
	for (int i = 0; int(p0.size()) > i; ++i) {
		if (i > 0) {
			cout << ",";
		}
		cout << "\"" << p0[i] << "\"";
	}
	cout << "}" << "," << "{";
	for (int i = 0; int(p1.size()) > i; ++i) {
		if (i > 0) {
			cout << ",";
		}
		cout << "\"" << p1[i] << "\"";
	}
	cout << "}";
	cout << "]" << endl;
	Hieroglyphs *obj;
	int answer;
	obj = new Hieroglyphs();
	clock_t startTime = clock();
	answer = obj->minimumVisible(p0, p1);
	clock_t endTime = clock();
	delete obj;
	bool res;
	res = true;
	cout << "Time: " << double(endTime - startTime) / CLOCKS_PER_SEC << " seconds" << endl;
	if (hasAnswer) {
		cout << "Desired answer:" << endl;
		cout << "\t" << p2 << endl;
	}
	cout << "Your answer:" << endl;
	cout << "\t" << answer << endl;
	if (hasAnswer) {
		res = answer == p2;
	}
	if (!res) {
		cout << "DOESN'T MATCH!!!!" << endl;
	} else if (double(endTime - startTime) / CLOCKS_PER_SEC >= 2) {
		cout << "FAIL the timeout" << endl;
		res = false;
	} else if (hasAnswer) {
		cout << "Match :-)" << endl;
	} else {
		cout << "OK, but is it right?" << endl;
	}
	cout << "" << endl;
	return res;
}
int main() {
	bool all_right;
	bool disabled;
	bool tests_disabled;
	all_right = true;
	tests_disabled = false;

	vector <string> p0;
	vector <string> p1;
	int p2;

	{
	// ----- test 0 -----
	disabled = false;
	string t0[] = {"0 0 10 0,10 0 10 3"};
			p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
	string t1[] = {"0 1 10 1","5 1 5 4"};
			p1.assign(t1, t1 + sizeof(t1) / sizeof(t1[0]));
	p2 = 16;
	all_right = (disabled || KawigiEdit_RunTest(0, p0, p1, true, p2) ) && all_right;
	tests_disabled = tests_disabled || disabled;
	// ------------------
	}

	{
	// ----- test 1 -----
	disabled = false;
	string t0[] = {"1 1 1 5"};
			p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
	string t1[] = {"3 2 5 2"};
			p1.assign(t1, t1 + sizeof(t1) / sizeof(t1[0]));
	p2 = 6;
	all_right = (disabled || KawigiEdit_RunTest(1, p0, p1, true, p2) ) && all_right;
	tests_disabled = tests_disabled || disabled;
	// ------------------
	}

	{
	// ----- test 2 -----
	disabled = false;
	string t0[] = {"0 2 6 2"};
			p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
	string t1[] = {"5 1 6 1,8 1 9 1"};
			p1.assign(t1, t1 + sizeof(t1) / sizeof(t1[0]));
	p2 = 6;
	all_right = (disabled || KawigiEdit_RunTest(2, p0, p1, true, p2) ) && all_right;
	tests_disabled = tests_disabled || disabled;
	// ------------------
	}

	{
	// ----- test 3 -----
	disabled = false;
	string t0[] = {"10 20 10 30,15 20 15 30","10 20 15 20,0 30 30 30"};
			p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
	string t1[] = {"0 5 0 15,5 5 5 25","0 5 5 5,0 15 5 15"};
			p1.assign(t1, t1 + sizeof(t1) / sizeof(t1[0]));
	p2 = 65;
	all_right = (disabled || KawigiEdit_RunTest(3, p0, p1, true, p2) ) && all_right;
	tests_disabled = tests_disabled || disabled;
	// ------------------
	}

	{
	// ----- test 4 -----
	disabled = false;
	string t0[] = {"10 10 10 20,10 30 10 40","10 10 20 10"};
			p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
	string t1[] = {"10 0 10 20,10 27 10 35","10 0 20 0"};
			p1.assign(t1, t1 + sizeof(t1) / sizeof(t1[0]));
	p2 = 45;
	all_right = (disabled || KawigiEdit_RunTest(4, p0, p1, true, p2) ) && all_right;
	tests_disabled = tests_disabled || disabled;
	// ------------------
	}

	if (all_right) {
		if (tests_disabled) {
			cout << "You're a stud (but some test cases were disabled)!" << endl;
		} else {
			cout << "You're a stud (at least on given cases)!" << endl;
		}
	} else {
		cout << "Some of the test cases had errors." << endl;
	}
	return 0;
}
// END KAWIGIEDIT TESTING//Powered by KawigiEdit-pf 2.3.0
//With unused code cleaner (beta) by ahmed_aly
